#ifndef CEditNumH
#define CEditNumH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <StdCtrls.hpp>
#include <values.h>
#include <limits.h>
#include <math.h>
#include <clipbrd.hpp>
#include <string.h>
#include "CtrlStd.h"
#include "CEdit.h"
#include "uCalculatrice.h"

#define MAXMAX  999999999999999
#define MAXMASK 15

#define SEP_DEC         ','
#define SEP_DEC_2       '.'

enum TypeVal { eShInt, eUShInt, eInt, eUInt, eLong, eFloat, eDouble, eCurrency };

typedef bool __fastcall (__closure *TOnCorrectValue)(TObject* Sender, double *dIllegalValue);

class PACKAGE_CTRLSTD CEditNum : public CCustomEdit
    {
    // Variables de stockage
    private:
        TBitBtn         *m_pbCalc;
        TfCalculatrice  *Calc;
        
        typedef         CCustomEdit inherited;
        TColor          FColorNeg;
        TColor          FColorPositive;
        AnsiString      FMask;
        double          FMax;
        double          FMin;
        bool            FNSActive;
        enum TypeVal    FTypeVal;
        double          m_ValNS;   // Valeur NS suivant TypeValeur selectionne
        bool            FbCalc;
        TOnCorrectValue m_OnCorrectValue;
        TNotifyEvent    m_OnChange;
        TNotifyEvent    OldOnChange;
        TNotifyEvent    m_pOnExit;
        TNotifyEvent    m_pOnEnter;

    // Properties
    __published:
      __property              Alignment = {default = taRightJustify};
      __property bool         Calculatrice = { read=FbCalc,       write=SetFbCalc, default = false  };
      __property TColor       ColorNeg      = { read=FColorNeg,      write=SetColorNeg,      default = clRed       };
      __property TColor       ColorPositive = { read=FColorPositive, write=SetColorPositive, default = clWindowText};
      __property AnsiString   Mask        = { read=FMask,         write=SetMask,      default = NULL              };
		__property double       Max         = { read=FMax,          write=SetMax,       stored  = false             };
		__property double       Min         = { read=FMin,          write=SetMin,       stored  = false             };
		__property bool         NSActive    = { read=FNSActive,     write=SetNSActive,  default = false             };
     //StateNS peut seulement être affecté à true pas l'utilisateur
		__property bool         StateNS     = { read=GetStateNS,    write=SetStateNS                                };
		__property AnsiString   Text        = { read=GetText,       write=SetText};
		__property enum TypeVal TypeValeur  = { read=FTypeVal,      write=SetTypeVal,   default = eDouble           };
		__property double       Valeur      = { read=GetValue,      write=SetValue,     stored  = false             };
		__property double       ValeurNS    = { read=m_ValNS ,      write=SetValNS,     stored  = false             };
      __property Anchors;
      __property AutoSelect;
      __property AutoSize;
      __property BorderStyle;
      __property Color;
//      __property CharCase;
      __property Ctl3D;
      __property DragCursor;
      __property DragMode;
      __property Enabled;
      __property Filtres;
      __property Font;
      __property HelpContext;
      __property Hint;
      __property Libelle;
      __property LibelleFont;
      __property Modified;
      __property OEMConvert;
//      __property ParentColor;
      __property ParentFont;
      __property ParentShowHint;
      __property PasswordChar;
      __property PopupMenu;
      __property ReadOnly;
      __property ShowHint;
      __property SizeEspace;
      __property TabOrder;
      __property TabStop;
      __property Visible;

 __published:
      // Publication des événements hérités
      __property TNotifyEvent  OnChange = {read = m_OnChange, write = SetOnChange};
      __property TNotifyEvent       OnExit     = { read=m_pOnExit           , write=m_pOnExit             };
      __property TNotifyEvent       OnEnter    = { read=m_pOnEnter          , write=m_pOnEnter            };
      __property OnStartDrag;
      __property OnDragDrop;
      __property OnDragOver;
      __property OnEndDrag;
      __property OnIllegalChar;
      __property OnIllegalPaste;
      __property OnKeyDown;
      __property OnKeyPress;
      __property OnKeyUp;
      __property OnMouseDown;
      __property OnMouseMove;
      __property OnMouseUp;
      __property OnClick;
      __property OnDblClick;

      __property TOnCorrectValue OnCorrectValue = {read = m_OnCorrectValue, write = m_OnCorrectValue};

	// Fonctions relatives aux properties
	private:
      void        __fastcall PerdreLaDonnee(signed int Inutilise);
		void        __fastcall SetMask( const AnsiString aMask       );
		void        __fastcall SetMax(      double          dMax        );
		void        __fastcall SetMin(      double          dMin        );
		void        __fastcall SetNSActive( bool            bNSActive   );
		bool        __fastcall GetStateNS() /*const*/;
		void        __fastcall SetStateNS(  bool            bStateNS   );
		void 		__fastcall SetText( const AnsiString aText       );
		void        __fastcall SetTypeVal(  enum TypeVal    eTypeValeur );
		void 		__fastcall SetValue(    double          Value       );
		void        __fastcall SetValNS(    double          Value       );
		int         __fastcall GetMaxLength() const;
        void        __fastcall SetFbCalc( bool bCalc );
		AnsiString  __fastcall GetText();
		double      __fastcall GetValue();
        double      __fastcall GetValNS() const;
      TColor __fastcall SetColorNeg     (TColor CouleurNegative);
      TColor __fastcall SetColorPositive(TColor CouleurPositive);
      void __fastcall MyOnChange (TObject* Sender);
      void __fastcall SetOnChange(TNotifyEvent OnChange);
      void __fastcall MyOnEnter (TObject* Sender);
      void __fastcall MyOnExit  (TObject* Sender);

      void __fastcall sbCalcClick( TObject *pSender);
	// Variables internes
	private:
		bool bFlagExec;

	// Fonctions internes
	private:
		void        __fastcall SetMaxLength();
		AnsiString Concatene( const AnsiString &aText ) const;
		AnsiString Format( const AnsiString &aText);
		double Arrond( double d, int n);
      bool __fastcall DecompNum( const AnsiString &aNum, int *_iNbEntiers, int *_iNbDecim, int *_iPosDec);
		bool __fastcall IsClipboardValid();
		bool __fastcall IsNumValid( const AnsiString &aNum );
		bool __fastcall IsNumInMiddle(const AnsiString &aText, char Key );
		int  __fastcall CalcNbSpaces();
      void __fastcall PositionnerBoutonCalculette();
      void __fastcall AfficherBoutonCalculette(bool bAfficher);
      double __fastcall GetDefaultValNS() const;

      // Fonctions de base d'écriture dans la dfm
      void __fastcall WriteInteger(       int  nValue, TStream *Stream);
      void __fastcall WriteString (const AnsiString &szValue, TStream *Stream);
      // Fonctions de base de lecture dans la dfm
      int        __fastcall ReadInteger(TStream *Stream);
      AnsiString __fastcall ReadString (TStream *Stream);

      void __fastcall OnSetValue(const AnsiString &aValue);
      AnsiString __fastcall GetMaskNumeric() const;
      AnsiString __fastcall DoubleToAnsi(double dNombre);
      double __fastcall AnsiToDouble(AnsiString aNombre);
      bool __fastcall ConcatenerSiBesoin(AnsiString *aNombre, int iClientWidth);

	protected:
		DYNAMIC void __fastcall KeyDown( Word &Key, Classes::TShiftState Shift);
		DYNAMIC void __fastcall KeyPress( char &Key );
		DYNAMIC void __fastcall KeyUp  ( Word &Key, Classes::TShiftState Shift);
      virtual void __fastcall WndProc( TMessage &Message );

        virtual void __fastcall DefineProperties(TFiler* Filer);

        void __fastcall ReadMax(TReader* Reader);
        void __fastcall WriteMax(TWriter* Writer);
        void __fastcall ReadMin(TReader* Reader);
        void __fastcall WriteMin(TWriter* Writer);
        void __fastcall ReadValeurNS(TReader* Reader);
        void __fastcall WriteValeurNS(TWriter* Writer);
        virtual void __fastcall ReadDatas (TStream *Stream);

	public:
		__property signed int   MaxLength   = { read=GetMaxLength,  write=PerdreLaDonnee, stored = false};

		__fastcall CEditNum(TComponent* Owner);
    virtual bool __fastcall EstCorrectementRempliDefault( void);
	};
#endif
